//
//  WMETextTableViewCell.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/1/22.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMETextTableViewCell.h"
#import "PrefixHeader.pch"
#import <UIImageView+WebCache.h>
#import <Masonry.h>
#pragma mark - 没有图片的cell 
@interface WMETextTableViewCell ()
@property (nonatomic, retain)UILabel *content;/**<主要内容*/
@property (nonatomic, retain)UIImageView *xiaoxi;/**<左上角的图标*/
@property (nonatomic, retain)UILabel *comment_count;/**<评论的人数*/
@property (nonatomic, retain)UILabel *userName;/**<发布者的名字*/
@property (nonatomic, retain)UILabel *clubName;/**<俱乐部名字*/
@property (nonatomic, retain)UILabel *time;/**<距今何年*/
@property (nonatomic, retain)UILabel *line;/**<分割线*/

@end
@implementation WMETextTableViewCell
- (void)dealloc
{
    [_model release];
    [_content release];
    [_xiaoxi release];
    [_comment_count release];
    [_userName release];
    [_clubName release];
    [_time release];
    [_line release];
    [super dealloc];
}

#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //文本内容
        self.content = [[UILabel alloc] init];
//        _content.backgroundColor = [UIColor yellowColor];
        _content.font = [UIFont systemFontOfSize:13];
        _content.numberOfLines = 2;
        [self.contentView addSubview:_content];
        [_content release];
        
        //左上角消息的背景
        self.xiaoxi = [[UIImageView alloc] init];
//        _xiaoxi.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_xiaoxi];
        [_xiaoxi release];
        
        //左上角的消息的个数
        self.comment_count = [[UILabel alloc] init];
//        _comment_count.backgroundColor = [UIColor redColor];
        _comment_count.font = [UIFont systemFontOfSize:7];
        _comment_count.textColor = [UIColor grayColor];
        _comment_count.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_comment_count];
        [_comment_count release];
        
        //用户名
        self.userName = [[UILabel alloc] init];
//        _userName.backgroundColor = [UIColor redColor];
        _userName.font = [UIFont systemFontOfSize:10];
        _userName.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_userName];
        [_userName release];
        
        //俱乐部名
        self.clubName = [[UILabel alloc] init];
//        _clubName.backgroundColor = [UIColor yellowColor];
        _clubName.font = [UIFont systemFontOfSize:10];
        _clubName.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_clubName];
        [_clubName release];
        
        self.time = [[UILabel alloc] init];
//        _time.backgroundColor = [UIColor blueColor];
        _time.font = [UIFont systemFontOfSize:9];
        _time.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_time];
        [_time release];
        
        //自己画的tableView的分隔线
        self.line = [[UILabel alloc] init];
        _line.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_line];
        [_line release];
    }
    return self;
}
#pragma mark - 重写set方法
- (void)setModel:(WMECludModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
        _content.text = _model.content;
        
        _comment_count.text = _model.comment_count.stringValue;
        if (_comment_count.text.integerValue > 1000) {
            _comment_count.text = [NSString stringWithFormat:@"%ldk", _comment_count.text.integerValue / 1000];
        }

        _time.text = [[self class]compareCurrentTime:_model.active_time];
        _userName.text = _model.userName;
        
        _clubName.text = _model.cludName;
    }
}

#pragma mark - 设置布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    _xiaoxi.frame = CGRectMake(10, 15, 25, 25);
    _xiaoxi.image = [UIImage imageNamed:@"xiaoxi"];
//    __block __typeof(&*self)bs = self;
//    [_xiaoxi mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(25, 25));
//        make.left.equalTo(bs.mas_left).with.offset(10);
//        make.top.equalTo(bs.mas_top).with.offset(15);
//    }];
    
    _comment_count.frame = CGRectMake(10, 18, 25, 15);
//    [_comment_count mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(15, 15));
//        make.left.equalTo(bs.mas_left).with.offset(16);
//        make.top.equalTo(bs.mas_top).with.offset(18);
//    }];
    CGFloat textHeight = [[self class] heightWithText:_content.text];
    if (textHeight > 15.51 * 2) {
        textHeight = 15.51 * 2 + 1;
    }
    _content.frame = CGRectMake(40, 20, WIDTH - 40 - 10, textHeight);
//    [_content mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(bs.mas_left).with.offset(40);
//        make.right.equalTo(bs.mas_right).with.offset(-10);
//        make.top.equalTo(bs.mas_top).with.offset(20);
//        make.height.equalTo(@(textHeight));
//    }];
    CGFloat textWidth = [[self class] widthWithText:_userName.text MySize:CGSizeMake(WIDTH - 100, 10) MyFontSize:10];
    _userName.frame = CGRectMake(40, _content.frame.size.height + _content.frame.origin.y + 15, textWidth, 10);
//    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(_content.mas_bottom).with.offset(20);
//        make.left.equalTo(bs.mas_left).with.offset(40);
//        make.height.equalTo(@10);
//        make.width.equalTo(@(textWidth + 10));
//    }];
    
    CGFloat timeWidth = [[self class] widthWithText:_time.text MySize:CGSizeMake(WIDTH - 200, 10) MyFontSize:9];
    _time.frame = CGRectMake(WIDTH - timeWidth - 10, textHeight + 30, timeWidth, 10);
//    [_time mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(bs.mas_right).with.offset(10);
//        make.left.equalTo(bs.mas_right).with.offset(timeWidth + 10);
//        make.top.equalTo(_content.mas_bottom).with.offset(20);
//        make.height.equalTo(@10);
//    }];
    _clubName.frame = CGRectMake(40 + textWidth + 10, _content.frame.size.height + _content.frame.origin.y + 15, 100, 10);
//    [_clubName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(_userName.mas_right).with.offset(10);
//        make.top.equalTo(_content.mas_bottom).with.offset(20);
//        make.right.equalTo(_time.mas_left).with.offset(-10);
//        make.height.equalTo(@10);
//    }];

    _line.frame = CGRectMake(40, _clubName.frame.origin.y + _clubName.frame.size.height + 15 , WIDTH - 40, 0.3);
//    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(bs.mas_left).with.offset(40);
//        make.top.equalTo(_clubName.mas_bottom).with.offset(9.5);
//        make.right.equalTo(bs.mas_right).with.offset(0);
//        make.height.equalTo(@0.5);
//    }];
}
//计算文本高
+ (CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    //最大范围
    CGSize size = CGSizeMake(WIDTH - 50, 1000);
    //计算 文本 需的rectangular
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回文本 的 高
    return r.size.height;
}
//计算文本宽
+ (CGFloat)widthWithText:(NSString *)text MySize:(CGSize)size MyFontSize:(CGFloat)fontSize
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
//    CGSize size = CGSizeMake(WIDTH - 100, 10);
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return r.size.width;
}


//计算时间 转换成 距离现在 多久
+ (NSString *)compareCurrentTime:(NSNumber *)date
{
    // 返回以1970/01/01 GMT为基准，然后过了secs秒的时间
    NSDate *compareDate = [NSDate dateWithTimeIntervalSince1970:[date doubleValue]];
    // 以当前时间(Now)为基准时间，返回实例保存的时间与当前时间(Now)的时间间隔
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval / 60) < 60){
        result = [NSString stringWithFormat:@"%ld分钟前", temp];
    }
    
    else if((temp = temp / 60) < 24){
        result = [NSString stringWithFormat:@"%ld小时前", temp];
    }
    
    else if((temp = temp / 24) < 30){
        result = [NSString stringWithFormat:@"%ld天前", temp];
    }
    
    else if((temp = temp / 30) < 12){
        result = [NSString stringWithFormat:@"%ld月前", temp];
    }
    else{
        temp = temp / 12;
        result = [NSString stringWithFormat:@"%ld年前", temp];
    }
    
    return  result;
}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
